package com.lnj.messages.quarzJob;

import com.lnj.messages.servise.HybridRecommenderService;
import com.lnj.messages.utiles.JsonUtil;
import com.lnj.utils.RedisKeys;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.List;
import java.util.concurrent.TimeUnit;

public class RecommenderJob implements Job {
    @Autowired
    private HybridRecommenderService hybridRecommenderService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private JsonUtil jsonUtil;

    @Override
    public void execute(JobExecutionContext context) {
        String object =(String) context.getJobDetail().getJobDataMap().get("userId");
        Long userId = Long.valueOf(object);
        List<Long> recommendItems = hybridRecommenderService.recommendItems(userId, 50);
        if (recommendItems!=null){
            stringRedisTemplate.opsForValue().set(RedisKeys.USER_RECOMMEND+userId,jsonUtil.toJson(recommendItems));
            stringRedisTemplate.expire(RedisKeys.USER_RECOMMEND+userId,1, TimeUnit.DAYS);
            System.out.println("推荐商品已更新");
        }else {
            System.out.println("推荐商品为空");
        }
    }
}
